/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ OrderSettingServiceImpl</dd>
 * <dd>创建时间：  10:55 2021/1/9</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/1/9      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * @Author: guodong
     * @Date: 10:54 2021/1/9
     * @Parms [year, month]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询预约数据
     */
    @Override
    public List<OrderSetting> findByMonth(Integer year, Integer month) {
        log.info("[预约设置-查询预约数据]year: {}, month: {}", year, month);
        /*
            开始时间： 2021-01-01
            结束时间： 2021-01-31
         */
        String beginDate = String.format("%d-%d-1", year, month);
        String endDate = String.format("%d-%d-31", year, month);
        log.info("[预约设置-查询预约数据] beginDate: {} endDate: {}", beginDate, endDate);
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //大于等于开始时间
        criteria.andGreaterThanOrEqualTo("orderDate", beginDate);
        //小于等于结束时间
        criteria.andLessThanOrEqualTo("orderDate", endDate);
        List<OrderSetting> orderSettingList = orderSettingMapper.selectByExample(example);
        return orderSettingList;
    }

    /**
     * @Author: guodong
     * @Date: 11:49 2021/1/9
     * @Parms [orderSetting]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 设置预约人数
     */
    @Override
    @Transactional //事务注解
    public void editNumByDate(OrderSetting orderSetting) {
        log.info("[设置预约人数]data: {}", orderSetting);
        //1.存在则修改
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderDate", orderSetting.getOrderDate());
        int i = orderSettingMapper.updateByExampleSelective(orderSetting, example);
        log.info("[根据日期更新条数： {}]", i);
        //2.不存在则新增
        if(i==0){//如果i=0说明没有该orderDate中的数据
            orderSettingMapper.insertSelective(orderSetting);
        }
    }

    /**
     * @Author: guodong 
     * @Date: 14:51 2021/1/9
     * @Parms [orderSettingList]
     * @ReturnType: void
     * @Description: 批量上传设置预约人数
    */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            orderSettingMapper.insertSelective(orderSetting);
        }
    }
}
